package cn.xfdzx.android.apps.shop.video;


import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.ItemVideoGoodAdapter;
import cn.xfdzx.android.apps.shop.adapter.OrderListAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.AddLiveGoodsBeen;
import cn.xfdzx.android.apps.shop.bean.OrderListBeen;
import cn.xfdzx.android.apps.shop.bean.StoreGoodsListBeen;
import cn.xfdzx.android.apps.shop.contract.VideoGoodContract;
import cn.xfdzx.android.apps.shop.presenter.VideoGoodPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.Utils;

//直播间添加商品
public class VideoShopGoodActivity extends BaseMvpActivity<VideoGoodPresenter> implements VideoGoodContract.View {

    @BindView(R.id.et_home_search)
    EditText etHomeSearch;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.rv_select_good)
    RecyclerView rvSelectGood;
    @BindView(R.id.tv_confirm_add)
    TextView tvConfirmAdd;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.ll_hint)
    LinearLayout llHint;


    private int page = 1;
    private int limit = Constants.LIMIT;
    private ItemVideoGoodAdapter itemVideoGoodAdapter;
    private List<StoreGoodsListBeen.DataBean.GoodsBean> mList = new ArrayList<>();

    private String keyword = "";
    private List<String> ids;

    @Override
    public int getLayoutId() {
        return R.layout.activity_video_shop_good;
    }

    @Override
    public void initView() {
        ids = getIntent().getStringArrayListExtra("ids");
        mPresenter = new VideoGoodPresenter();
        mPresenter.attachView(this);
        watchSearch();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvSelectGood.setLayoutManager(linearLayoutManager);
        rvSelectGood.setItemAnimator(new DefaultItemAnimator());
        itemVideoGoodAdapter = new ItemVideoGoodAdapter(this, mList, ids);
        rvSelectGood.setAdapter(itemVideoGoodAdapter);

        mPresenter.initStoreGoodsAddress(false, true, page + "", limit + "", keyword);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.initStoreGoodsAddress(true, true, page + "", limit + "", keyword);
            }
        });

    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {

            ProgressDialog.getInstance().dismiss();

    }

    @Override
    public void onError(Throwable throwable) {

    }
private int totalNum=0;
    @Override
    public void onInitSuccess(StoreGoodsListBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (page == 1) {
                mList.clear();
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.initStoreGoodsAddress(false, false, page + "", limit + "", keyword);
                    }
                });
            }
            totalNum=bean.getData().getGoods_num();
            if (bean.getData().getGoods().size() == 0) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
            }
            mList.addAll(bean.getData().getGoods());

            for (StoreGoodsListBeen.DataBean.GoodsBean dataBean : mList) {
                for (String id : ids) {
                    if (id.equals(dataBean.getId() + "")) {
                        dataBean.setIs_select(true);
                    }
                }
            }
            itemVideoGoodAdapter.notifyDataSetChanged();
            heji();
            if (mList.size() == 0) {
                llHint.setVisibility(View.VISIBLE);
            } else {
                llHint.setVisibility(View.GONE);
            }
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onAddLiveGoodsDataSuccess(AddLiveGoodsBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            finish();
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    public void heji() {
        tvConfirmAdd.setText(ids.size() + "/" + totalNum + "确认");
    }

    @OnClick({R.id.tv_cancel, R.id.iv_back, R.id.tv_confirm_add})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.tv_confirm_add:
                if (Utils.isFastClick()){
                String ids_data = "";
                for (String s : ids) {
                    if (ids_data.isEmpty()) {
                        ids_data = s;
                    } else {
                        ids_data = ids_data + "," + s;
                    }
                }
                mPresenter.addLiveGoods(getIntent().getStringExtra("roomId"), ids_data);}
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_cancel:
                if (Utils.isFastClick()) {
                    etHomeSearch.getText().clear();
                    keyword = "";
                    page = 1;
                    mRefreshLayout.setNoMoreData(false);
                    mPresenter.initStoreGoodsAddress(false, true, page + "", limit + "", keyword);
                }
                break;
        }
    }

    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }

    /**
     * @方法说明:监控软键盘的的搜索按钮
     * @方法名称:watchSearch
     * @返回值:void
     */
    public void watchSearch() {
        etHomeSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // 先隐藏键盘
                    ((InputMethodManager) etHomeSearch.getContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(VideoShopGoodActivity.this
                                            .getCurrentFocus().getWindowToken(),
                                    InputMethodManager.HIDE_NOT_ALWAYS);
                    // 搜索，进行自己要的操作...
//                    seachList(viewIndex);//这里是我要做的操作！
                    keyword = etHomeSearch.getText().toString().trim();
                    page = 1;
                    mRefreshLayout.setNoMoreData(false);
                    mPresenter.initStoreGoodsAddress(false, true, page + "", limit + "", keyword);

                    return true;
                }
                return false;
            }
        });
    }

    @Override
    protected void onDestroy() {
        mList.clear();
        ids.clear();
        mList = null;
        ids = null;
        super.onDestroy();
    }
}
